package com.atguigu.exer;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author yihu
 * @create 2021-03-05 11:36
 * @email:1057202547@qq.com
 * @dec:
 */
public class EmplopeTest {
    //使用自然排序
    @Test
    public  void test1(){
        TreeSet set = new TreeSet();
        Employe e1 = new Employe("liudehua",55,new MyDate(1965,5,8));
        Employe e2 = new Employe("zhangxueyou",43,new MyDate(1968,2,18));
        Employe e3 = new Employe("guofucheng",40,new MyDate(1975,3,20));
        Employe e4 = new Employe("liming",56,new MyDate(1955,4,9));
        Employe e5 = new Employe("liangchaowei",58,new MyDate(1975,6,10));


        //要实现排序，红黑树才知道怎么排放
        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        Iterator iterator = set.iterator();

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    //按生日的先后顺序排
    @Test
    public  void test2(){
        TreeSet set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof  Employe && o2 instanceof  Employe){
                    Employe e1 = (Employe)o1;
                    Employe e2 = (Employe)o2;
                    return  e1.getBirthday().compareTo(e2.getBirthday());
                }
                throw new RuntimeException("传入的数据类型不一致！");
            }
        });
        Employe e1 = new Employe("liudehua",55,new MyDate(1965,5,8));
        Employe e2 = new Employe("zhangxueyou",43,new MyDate(1968,2,18));
        Employe e3 = new Employe("guofucheng",40,new MyDate(1975,3,20));
        Employe e4 = new Employe("liming",56,new MyDate(1955,4,9));
        Employe e5 = new Employe("liangchaowei",58,new MyDate(1975,6,10));


        //要实现排序，红黑树才知道怎么排放
        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        Iterator iterator = set.iterator();

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
